<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="utf-8">
    <meta name="author" content="wa">
    <title></title>
    <script src="../js/jquery-3.1.1.js"></script>
    <style>
        #chooseImg img {
            width: 100px;
            height: 100px;
        }
        
        #chooseImg input {
            display: none;
        }
    </style>
</head>

<body>
    <form action="#">
        <div id="chooseImg">
            <img src="../img/add.png" alt="">
            <input type="file">
            <img src="../img/add.png" alt="">
            <input type="file">
            <img src="../img/add.png" alt="">
            <input type="file">
        </div>
        <div id="upload">
            <button type="button">添加</button>
            <button type="submit">上传</button>
        </div>
    </form>
</body>

</html>
<script>
    // 通过点击添加按钮，添加一张占位的img和input
    $(':button').click(function () {
        var img = document.createElement('img');
        img.src = '../img/add.png';
        $('#chooseImg').append(img);
        var input = document.createElement('input');
        input.type = 'file';
        $('#chooseImg').append(input);
        clickFile();
    });

    clickFile();
    function clickFile() {

        // 点击图片实现选择文件的功能
        $('#chooseImg').on('click','img',function () {
            $(this).next().click();
            // $(this).nextAll().off('click');
        })
    };


    $('#chooseImg').on('change', 'input', function () {
        // 取出上传的文件
        var file = this.files[0];
        // 如果是图片
        if (file.type.startsWith('image')) {
            // console.log(file);
            $(this).prev().attr('src', URL.createObjectURL(file));
        }
    });

</script>